<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js异步编程之Promise成功处理</title>
</head>
<body>
  <ul>
    <li>每个Promise微任务处理都是在上次Promise处理结束后继续处理，因此根据上次Promise处理结果(成功或失败)而定</li>
    <li>每个微任务Promise.then仍然返回的是Promise,可作为下次Promise处理的输入,跟函数的链式处理很相似</li>
    <li>每个微任务Promise.then不仅可以返回Promise,也可返回任意包含then方法的对象，类甚至匿名类</li>
  </ul>
  <script>
    {
      new Promise(resolve => {
        resolve('success')
      }).then(res1 => {
        return new Promise((resolve, reject) => {
          resolve('miracle')
          setTimeout(() => {
            reject('fail')
          }, 0)
        }).then(res2 => {
          return res1 + '->' + res2
        }, err => {
          return new Promise((resolve, reject) => {
            reject('fail again...')
          })
        })
      }, err => {
        console.log(err)
      }).then(res => {
        console.log(res)
      }, err => {
        console.log(err)
      })
    }
    {
      new Promise(resolve => {
        resolve('success')
      }).then(res => {
        // 1.对象
        // return {
        //   then(resolve, reject) {
        //     resolve(res + '->' + 'miracle')
        //   }
        // }
        // 2.类
        // class $Promise {
        //   then(resolve, reject) {
        //     resolve(res + '->' + 'miracle')
        //   }
        // }
        // return new $Promise()
        // 3.匿名类
        return class {
          static then(resolve, reject) {
            resolve(res + '->' + 'miracle')
          }
        }
      }).then(res => {
        console.log(res)
      }, err => {
        console.log(err)
      })
    }
  </script>
</body>
</html>
